#!/usr/bin/env python
# yongming.li 

import sys
import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PropertyHighlighter import*
import copy



propPath = os.path.dirname(__file__)
print 'propPath is : ' + propPath
sys.path.append('../'+propPath)

class AndroidProperty(object):
    def __init__(self, name, value):
        self.name =  name
        self.value =  value



class PropertyForm(QDialog):

    def  showandroidProp(self,name):
        self.box = QtGui.QGroupBox(name)
        layout = QtGui.QVBoxLayout()
        self.edit = QtGui.QTextEdit('')

        highlighter = PropertyHighlighter(self.edit.document())
        
        self.edit.setTextColor(QColor(0, 0, 255, 255))

        self.edit.setMinimumSize(300,300)
        layout.addWidget(self.edit)

        self.btn = QtGui.QPushButton('------------------------------')
        self.btn.setMaximumSize(350,10)
        layout.addWidget(self.btn)

        self.box.show()
        self.box.setLayout(layout)
        
        return self.box , self.edit , self.btn

    def  setProp(self):
        cmd = 'adb shell setprop '
        name = self.setnameedit.toPlainText()
        value = self.setvalueedit.toPlainText()
        cmd = 'adb shell setprop %s  %s'%(name,value)
        # fprintf(stderr,"usage: setprop <key> <value>\n");
        print cmd
        os.system(cmd)
        
    def  showsetandroidProp(self,name):
        self.setbox = QtGui.QGroupBox(name)
        vlayout = QtGui.QVBoxLayout()
        hlayout = QtGui.QHBoxLayout()
        self.setnameedit = QtGui.QTextEdit('')
        self.setvalueedit = QtGui.QTextEdit('')
        self.setnameedit.setMinimumSize(300,30)
        self.setvalueedit.setMinimumSize(200,30)
   
        self.setnameedit.setTextColor(QColor(0, 0, 255, 255))
        self.setvalueedit.setTextColor(QColor(255, 0, 0, 255))
        
        self.setbtn = QtGui.QPushButton('set property')
        #self.edit.setMinimumSize(350,300)
        self.connect(self.setbtn, SIGNAL('clicked()'), self.setProp)
        
        hlayout.addWidget(self.setvalueedit)
        hlayout.addWidget(self.setbtn)
        
        vlayout.addWidget(self.setnameedit)
        vlayout.addLayout(hlayout)

        
        

        self.setbox.show()
        self.setbox.setLayout(vlayout)
    def clearProp(self):
        self.persistedit.clear()
        self.readonlyedit.clear()
        self.svcedit.clear()
        self.noramledit.clear()
        #self.changededit.clear()

    def refreshProp(self):
        self.clearProp()
        cmd = 'adb shell getprop'
        lines = os.popen(cmd).readlines()

        for  line  in lines: 
            line = line.strip()
            if len (line) == 0 :
                continue
            items = line.split(':')

            name = items[0].strip()
            value = items[1].strip()
            self.newprop[name]=value
            if line.startswith('[ro.'):
                self.readonlyedit.append(name + ' is ' + value)
                continue
            if line.startswith('[persist.'):
                self.persistedit.append(name + ' is ' + value)
                continue
            if line.startswith('[init.svc.'):
                self.svcedit.append(name + ' is ' + value)
                continue
            self.noramledit.append(name + ' is ' + value)
            

        if len(self.oldprop) == 0:
            print 'old is zero'
        else:
            for item in self.newprop:
                print self.newprop[item]
                if self.newprop[item] != self.oldprop[item]:
                    print item
                    print 'shit'
                    self.changededit.append(item+' changed from '+self.oldprop[item]+' to '+ self.newprop[item])
        self.oldprop = copy.deepcopy(self.newprop)   
        print 'refreshProp'
                  
    def __init__(self, parent=None):

        super(PropertyForm, self).__init__(parent)
        self.frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel
        #self.showReadonlyProp()
        #self.showpersistProp()
        self.showsetandroidProp('set property')
        self.normalbox , self.noramledit , self.normalbtn = self.showandroidProp('normal prop ')
        self.svcbox , self.svcedit , self.svcbtn = self.showandroidProp('service prop ')
        # ro.* properties may NEVER be modified once set
        self.readonlybox , self.readonlyedit , self.readonlybtn = self.showandroidProp('read only prop ')
        self.persistbox , self.persistedit , self.persistbtn = self.showandroidProp('persist prop ')
        
        self.changedbox , self.changededit , self.changedbtn = self.showandroidProp('changed prop ')

        self.refreshbtn = QtGui.QPushButton('refresh property')
        self.clearbtn = QtGui.QPushButton('clear property')
        #self.btn.setMaximumSize(350,10)
        self.connect(self.refreshbtn, SIGNAL('clicked()'), self.refreshProp)
        self.connect(self.clearbtn, SIGNAL('clicked()'), self.clearProp)
        
        mainLayout = QGridLayout()
        diffLayout = QGridLayout()
        self.setWindowTitle("Android Property")

        mainLayout.addWidget(self.readonlybox,0,0)
        mainLayout.addWidget(self.persistbox,0,1)
        mainLayout.addWidget(self.normalbox,1,0)
        mainLayout.addWidget(self.svcbox,1,1)
        mainLayout.addWidget(self.setbox,2,0)
        mainLayout.addWidget(self.refreshbtn,2,1)
        #mainLayout.addWidget(self.changedbox,0,2)
        #mainLayout.addWidget(self.clearbtn,2,2)
        diffLayout.addLayout(mainLayout,0,0)
        diffLayout.addWidget(self.changedbox,0,1)
       
        self.setLayout(diffLayout)
        self.show()

        self.newprop ={}
        self.oldprop ={}
        self.refreshProp()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = None
    form = PropertyForm()
    sys.exit(app.exec_())


